package com.changgou.seckill.task;

import com.changgou.entity.IdWorker;
import com.changgou.entity.SeckillStatus;
import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.dao.SeckillOrderMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.pojo.SeckillOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ThreadCreateOrder {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private StringRedisTemplate redisTemplate1;

    @Async
    public void createOrder() {
        try {
            System.out.println("休眠一下下单");
            Thread.sleep(10000);

            SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundListOps("SeckillOrderQueue").rightPop();

            if (seckillStatus == null) {
                return;
            }

            String time = seckillStatus.getTime();
            Long id = seckillStatus.getGoodsId();
            String username = seckillStatus.getUsername();

//            Object o = redisTemplate.boundListOps("SeckillGoodsCountList_" + id).rightPop();
//
//            if (o == null) {
//                clearUserQueue(username);
//                return;
//            }


            String key = "SeckillGoods_" + time;
            SeckillGoods seckillGoods= (SeckillGoods) redisTemplate.boundHashOps(key).get(id);

            if(redisTemplate1.opsForValue().increment("StockCount",-1)<0){
                redisTemplate1.opsForValue().set("StockCount", "0");
                throw new RuntimeException("已售空!");
            }

//            if (seckillGoods == null || seckillGoods.getStockCount() < 0) {
//                throw new RuntimeException("已售空!");
//            }

            SeckillOrder seckillOrder = new SeckillOrder();
            seckillOrder.setId(idWorker.nextId());
            seckillOrder.setSeckillId(id);
            seckillOrder.setMoney(seckillGoods.getCostPrice());
            seckillOrder.setUserId(username);
            seckillOrder.setCreateTime(new Date());
            seckillOrder.setStatus("0");
            System.out.println("线程"+Thread.currentThread().getId()+"来抢单====="+username);
            redisTemplate.boundHashOps("SeckillOrder").put(username, seckillOrder);

            seckillGoods.setStockCount(Integer.valueOf(redisTemplate1.opsForValue().get("StockCount")));

            if (seckillGoods.getStockCount() <= 0) {
                redisTemplate.boundHashOps(key).delete(id);
            }else{
                redisTemplate.boundHashOps(key).put(id, seckillGoods);
            }
            System.out.println("=====线程==="+Thread.currentThread().getId()+"==="+username+"===修改当前库存为=="+seckillGoods.getStockCount());
            seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);

            seckillStatus.setOrderId(seckillOrder.getId());
            seckillStatus.setMoney(Float.valueOf(seckillGoods.getCostPrice()));
            seckillStatus.setStatus(2);
            redisTemplate.boundHashOps("UserQueueStatus").put(username, seckillStatus);

            System.out.println("下单完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clearUserQueue(String username) {
         redisTemplate.boundHashOps("UserQueueCount").delete(username);
         redisTemplate.boundHashOps("UserQueueStatus").delete(username);
    }
}
